package com.ruoyi.system.controller;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;

public class Duom {
    public static void main(String[] args) {

        String dateTimeString = "2024-07-01 11:56:20";

        // 首先根据空格分割日期和时间
        String[] dateAndTime = dateTimeString.split(" ");

        // dateAndTime[0] 是日期部分，dateAndTime[1] 是时间部分
        String date = dateAndTime[0];
        String time = dateAndTime[1];

        // 再分别对日期和时间进行分割
        // 日期部分根据"-"分割
        String[] dateParts = date.split("-");
        int year = Integer.parseInt(dateParts[0]);
        int month = Integer.parseInt(dateParts[1]);
        int day = Integer.parseInt(dateParts[2]);

        // 时间部分根据":"分割
        String[] timeParts = time.split(":");
        int hour = Integer.parseInt(timeParts[0]);
        int minute = Integer.parseInt(timeParts[1]);
        int second = Integer.parseInt(timeParts[2]);

        // 输出结果
        System.out.println("年: " + year);
        System.out.println("月: " + month);
        System.out.println("日: " + day);
        System.out.println("时: " + hour);
        System.out.println("分: " + minute);
        System.out.println("秒: " + second);


        // 示例时间：2024-07-01 11:56:20 进入，2024-07-02 23:09:11 离开
        LocalDateTime enterTime = LocalDateTime.of(year, month, day, hour, minute, second);
        LocalDateTime leaveTime = LocalDateTime.of(2024, 7, 2, 23, 9, 11);

        double fee = calculateParkingFee(enterTime, leaveTime);
        System.out.println("停车费用为：" + fee + "元");
    }

    public static double calculateParkingFee(LocalDateTime enterTime, LocalDateTime leaveTime) {
        // 定义时间段和费用
        LocalTime startDayRate = LocalTime.of(8, 0);
        LocalTime endDayRate = LocalTime.of(17, 0);
        LocalTime startNightRate = LocalTime.of(18, 0);
        LocalTime endNightRate = LocalTime.of(7, 0);

        double dayRate = 5.0;
        double nightRate = 2.0;

        double totalFee = 0.0;

        // 转换时间为仅时间部分，便于比较
        LocalTime enterHour = enterTime.toLocalTime();
        LocalTime leaveHour = leaveTime.toLocalTime();

        // 处理进入时间到当天结束的情况
        if (!enterHour.isBefore(startDayRate)) {
            // 如果进入时间就在白天时段内
            LocalDateTime endOfDay = enterTime.toLocalDate().atTime(endDayRate);
            if (!endOfDay.isBefore(leaveTime)) {
                // 如果离开时间也在当天白天时段内
                long hours = ChronoUnit.HOURS.between(enterHour, leaveHour.isBefore(endOfDay.toLocalTime()) ? leaveHour : endOfDay.toLocalTime());
                totalFee += hours * dayRate;
                if (!leaveHour.isBefore(endOfDay.toLocalTime())) {
                    // 还需要计算夜晚的费用
                    leaveTime = leaveTime.minusHours(hours);
                } else {
                    // 离开时间已处理完，结束
                    return totalFee;
                }
            } else {
                // 离开时间在当天白天时段前
                long hours = ChronoUnit.HOURS.between(enterHour, endOfDay.toLocalTime());
                totalFee += hours * dayRate;
                leaveTime = endOfDay; // 更新离开时间为当天结束时间，以便处理跨夜
            }
        }

        // 处理跨夜或直接进入夜晚时段
        while (!leaveTime.isBefore(enterTime.toLocalDate().plusDays(1).atStartOfDay())) {
            // 计算夜晚费用
            LocalDateTime startNight = (leaveTime.toLocalDate().equals(enterTime.toLocalDate()) ? LocalDateTime.of(leaveTime.toLocalDate(), startNightRate) : LocalDateTime.of(leaveTime.toLocalDate(), LocalTime.MIDNIGHT));
            LocalDateTime endNight = LocalDateTime.of(leaveTime.toLocalDate().plusDays(1), endNightRate);

            long hours = ChronoUnit.HOURS.between(leaveTime.isAfter(startNight) ? leaveTime : startNight,
                    endNight.isAfter(leaveTime) ? leaveTime : endNight);

            totalFee += hours * nightRate;

            if (leaveTime.isAfter(endNight)) {
                // 如果离开时间还在下一天，继续循环
                leaveTime = leaveTime.minusHours(hours);
            } else {
                // 离开时间已处理完，结束
                break;
            }
        }

        return totalFee;
    }
}

